package cn.tedu.suppermemory.controller;

import cn.tedu.suppermemory.pojo.dto.TeacherAddDTO;
import cn.tedu.suppermemory.pojo.dto.TeacherUpdateDTO;
import cn.tedu.suppermemory.pojo.vo.TeacherListItemVO;
import cn.tedu.suppermemory.pojo.vo.TeacherStandardVO;
import cn.tedu.suppermemory.service.ITeacherService;
import cn.tedu.suppermemory.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


/**
 * 处理老师相关请求的控制器类
 *
 * @author java@tedu.cn
 * @version 0.0.1
 */
@Api(tags = "000. 老师管理模块")
@Slf4j
@RestController
@RequestMapping("/teachers")
@Validated
public class TeacherController {

    @Autowired
    private ITeacherService teacherService;

    public TeacherController() {
        log.debug("创建控制器类对象：TeacherController");
    }

    // http://localhost:8888/teachers/add-new
    @PostMapping("/add-new")
    @ApiOperation("添加老师")
    @ApiOperationSupport(order = 100)
    public JsonResult<Void> addNew(@Valid TeacherAddDTO teacherAddDTO) {
        log.debug("开始处理【添加相册】的请求，参数：{}", teacherAddDTO);
        teacherService.addNew(teacherAddDTO);
        return JsonResult.ok();
    }

    // http://localhost:8888/teachers/999/delete
    @PostMapping("/{id:[0-9]+}/delete")
    @ApiOperation("根据id删除老师")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id", value = "老师ID", required = true, example = "999", dataType = "long")
    public JsonResult<Void> delete(@Range(min = 1, max = 1000, message = "删除老师失败，老师ID非法！")
                                   @PathVariable Long id) {
        log.debug("开始处理【根据id删除老师】的请求，参数：{}", id);
        teacherService.delete(id);
        return JsonResult.ok();
    }

    // http://localhost:8888/teachers/999/update
    @PostMapping("/{id:[0-9]+}/update")
    @ApiOperation("修改老师详情")
    @ApiOperationSupport(order = 300)
    public JsonResult<Void> updateInfoById(@PathVariable Long id, TeacherUpdateDTO teacherUpdateDTO) {
        log.debug("开始处理【修改老师详情】的请求，参数：{}", teacherUpdateDTO);
        teacherService.updateInfoById(id, teacherUpdateDTO);
        return JsonResult.ok();
    }

    // http://localhost:8888/teachers/999
    @GetMapping("/{id:[0-9]+}")
    @ApiOperation("根据id查询老师详情")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParam(name = "id", value = "老师ID", required = true, example = "999", dataType = "long")
    public JsonResult<TeacherStandardVO> getStandardById(@Range(min = 1, max = 1000, message = "查询老师详情失败，老师ID非法！")
                                                       @PathVariable Long id) {
        log.debug("开始处理【根据id查询老师详情】的请求，参数：{}", id);
        TeacherStandardVO teacherStandardVO = teacherService.getStandardById(id);
        return JsonResult.ok(teacherStandardVO);
    }

    // http://localhost:8888/teachers
    @GetMapping("")
    @ApiOperation("查询老师列表")
    @ApiOperationSupport(order = 420)
    public JsonResult<List<TeacherListItemVO>> list() {
        log.debug("开始处理【查询老师列表】的业务，无参数");
        List<TeacherListItemVO> list = teacherService.list();
        return JsonResult.ok(list);
    }


}
